package utils;

import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

/**
 * @author wei kunJie
 * @date 2024/4/21 20:10:37
 */
object TimeStringUtil {
    fun getTimeString(dateTimeString: String): String {
        if (dateTimeString.isEmpty()) return ""
        val dateTimeString1 = dateTimeString.substring(0, 19)
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
        val dateTime = LocalDateTime.parse(dateTimeString1, formatter)
        val today = LocalDate.now()
        val yesterday = today.minusDays(1)
        val dayBeforeYesterday = today.minusDays(2)

        return when {
            dateTime.toLocalDate() == today -> dateTimeString1.substring(11, 16)
            dateTime.toLocalDate() == yesterday -> "昨天"
            dateTime.toLocalDate() == dayBeforeYesterday -> "前天"
            else -> dateTime.toLocalDate().toString().replace("-", "/")
        }
    }

    /**
     * 看dateTimeString1是不是在dateTimeString2之后
     */
    fun compareTime(dateTimeString1: String, dateTimeString2: String): Boolean {
        if (dateTimeString1.isEmpty()) return false
        if (dateTimeString2.isEmpty()) return false
        val dateTimeStr1 = dateTimeString1.substring(0, 19)
        val dateTimeStr2 = dateTimeString2.substring(0, 19)
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
        val dateTime1 = LocalDateTime.parse(dateTimeStr1, formatter)
        val dateTime2 = LocalDateTime.parse(dateTimeStr2, formatter)
        return dateTime1 > dateTime2
    }

    fun getDateTime(dateTimeString: String): LocalDateTime? {
        if (dateTimeString.isEmpty()) return null
        val dateTimeString1 = dateTimeString.substring(0, 19)
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
        return LocalDateTime.parse(dateTimeString1, formatter)
    }
}
